<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >

      <el-form-item label="打卡类型" prop="clockType">
        <el-select
          v-model="queryParams.clockType"
          placeholder="请选择打卡类型"
          clearable
        >
          <el-option label="上班" value="1" />
          <el-option label="下班" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['api:order:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['api:order:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      border
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="打卡地址" align="center" prop="address" />
      <el-table-column label="打卡时间" align="center" prop="clockTime" />
      <el-table-column label="打卡类型" align="center" prop="clockType">
        <template slot-scope="scope">
          <el-tag
            :type="getClockTypeTagType(scope.row.clockType)"
            :icon="getClockTypeIcon(scope.row.clockType)"
            size="small"
          >
            {{ getClockTypeText(scope.row.clockType) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="打开照片" align="center" prop="photoUrl">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photoUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listOrder,
  delOrder,
} from "@/api/api/driverclock";

export default {
  name: "DriverClock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 司机打卡信息表格数据
      orderList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        userId: null,
        clockType: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**
     * 查询司机打卡信息列表
     */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**
     * 搜索按钮操作
     */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /**
     * 重置按钮操作
     */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**
     * 多选框选中数据
     */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    /**
     * 删除按钮操作
     */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除司机打卡信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /**
     * 导出按钮操作
     */
    handleExport() {
      this.download(
        "api/order/export",
        {
          ...this.queryParams,
        },
        `driverclock_${new Date().getTime()}.xlsx`
      );
    },
    /**
     * 获取打卡类型文本
     * @param {string|number} clockType 打卡类型
     * @returns {string} 打卡类型文本
     */
    getClockTypeText(clockType) {
      const typeMap = {
        1: "上班",
        2: "下班",
        "1": "上班",
        "2": "下班",
      };
      return typeMap[clockType] || "未知";
    },
    /**
     * 获取打卡类型标签颜色
     * @param {string|number} clockType 打卡类型
     * @returns {string} 标签类型
     */
    getClockTypeTagType(clockType) {
      const typeMap = {
        1: "success",
        2: "warning",
        "1": "success",
        "2": "warning",
      };
      return typeMap[clockType] || "info";
    },
    /**
     * 获取打卡类型图标
     * @param {string|number} clockType 打卡类型
     * @returns {string} 图标类名
     */
    getClockTypeIcon(clockType) {
      const iconMap = {
        1: "el-icon-top",
        2: "el-icon-bottom",
        "1": "el-icon-top",
        "2": "el-icon-bottom",
      };
      return iconMap[clockType] || "el-icon-time";
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}
</style>
